package com.yonyou.gmmc.service.pointactivity.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.marketing.common.constants.ResultMsgConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.yonyou.gmmc.service.pointactivity.constant.PointActivityConstant;
import com.yonyou.gmmc.service.pointactivity.service.ActivityCommonService;
import com.yonyou.gmmc.service.pointactivity.service.ServiceStarService;
import com.yonyou.gmmc.service.pointactivity.util.StringUtils;

import io.swagger.annotations.Api;

@RestController
@RequestMapping(value = PointActivityConstant.BASE_PATH + "/" + PointActivityConstant.VERSION)
@Api("风采之星")
public class ServiceStarController {

	private Logger logger = Logger.getLogger(PointActivityController.class);
	
	@Autowired
	private ServiceStarService serviceStarService;
	
	@Autowired
	private ActivityCommonService activityCommonService;
	
	// 活动类型
	private Integer ACTIVITY_TYPE = 2;
	// 操作人
	private Long OPERATION_BY = 999999998L;
	
	/**
	 * 这个方法会在当前这个类(Controller)任何一个方法调用之前调用一次
	 * 
	 * @param req
	 */
	@ModelAttribute
	public void recordVisit(HttpServletRequest req) {
		
		String openId = req.getHeader(PointActivityConstant.HEADER_OPENID);
		try {
//			openId = null == openId ? "oXrcfuC1R4wosEPeJOHR7BPqaAOo" : openId;
			
			if (StringUtils.isNull(openId))
				throw new Exception("openId为空.!");

			logger.info("风采之星活动-记录用户访问次数.获取到的openId:" + openId);

			Map<String, Object> param = new HashMap<String, Object>();

			param.put("openId", openId);
			param.put("operationBy", OPERATION_BY);
			param.put("activityType", ACTIVITY_TYPE);
			param.put("longitude", StringUtils.isNull(req.getParameter("longitude")) ? "" : req.getParameter("longitude"));// 经度
			param.put("latitude", StringUtils.isNull(req.getParameter("latitude")) ? "" : req.getParameter("latitude"));// 纬度
			param.put("province", StringUtils.isNull(req.getParameter("province")) ? "" : req.getParameter("province"));

			activityCommonService.recordVisitInfo(param);
		} catch (Exception e) {
			logger.info("风采之星活动-记录用户访问次数.获取到的openId:" + openId + ",访问记录异常.");
		}
	}
	
	/**
	 * 进入投票页面之前新建投票人信息
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "/insertVoter", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public @ResponseBody Map<String, Object> insertVoter(HttpServletRequest req) {
		Map<String, Object> map = new HashMap<>();
		try {
			String openId = req.getHeader(PointActivityConstant.HEADER_OPENID);
//			openId = null == openId ? "oXrcfuC1R4wosEPeJOHR7BPqaAOo" : openId;
			if(openId == null || "".equals(openId)){
				map.put("message", "0");
				return map;
			}
			logger.info("服务之星活动获取到的openId为:=============" + openId);
			boolean ifVoter = serviceStarService.ifVoter(openId);
			if(ifVoter){
				serviceStarService.insertVoter(openId);
			}
			else{
				map.put("message", "1");
			}
		} catch (Exception e) {
			logger.info("已存在投票者信息，无需新增！");
			logger.info(e.getMessage());
			map.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_ERROR);
			map.put(ResultMsgConstants.ERR_MSG, "已存在投票者信息，无需新增！");
			map.put(ResultMsgConstants.QUERY_DATA, "");
		}
		return map;
	}
	
	@RequestMapping(value = "/getAdvisor", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public @ResponseBody Map<String, Object> getAdvisor(HttpServletRequest req) { 
		Map<String, Object> map = new HashMap<>();
		try {
			String openId = req.getHeader(PointActivityConstant.HEADER_OPENID);
//			openId = null == openId ? "oXrcfuC1R4wosEPeJOHR7BPqaAOo" : openId;
			String input = req.getParameter("input");
			Map<String, Object> param = new HashMap<>();
			param.put("min", StringUtils.isNull(req.getParameter("min")) ? "0" : req.getParameter("min"));
			param.put("max", StringUtils.isNull(req.getParameter("min")) ? serviceStarService.getCountAdvisor() : Integer.valueOf(req.getParameter("min")) + 3);
			param.put("openId", openId);
			param.put("input", input);
			map.put("resultList", serviceStarService.getAdvisor(param));
		} catch (Exception e) {
			logger.info("查询服务顾问信息失败！");
			logger.info(e.getMessage());
			map.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_ERROR);
			map.put(ResultMsgConstants.ERR_MSG, "查询服务顾问信息失败！");
			map.put(ResultMsgConstants.QUERY_DATA, "");
		}
		return map;
	}
	
	@RequestMapping(value = "/getOrder", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public @ResponseBody Map<String, Object> getAdvisorOrder(HttpServletRequest req) { 
		Map<String, Object> map = new HashMap<>();
		try {
			String openId = req.getHeader(PointActivityConstant.HEADER_OPENID);
//			openId = null == openId ? "oXrcfuC1R4wosEPeJOHR7BPqaAOo" : openId;
			String input = req.getParameter("input");
			Map<String, Object> param = new HashMap<>();
			param.put("min", StringUtils.isNull(req.getParameter("min")) ? "0" : req.getParameter("min"));
			param.put("max", StringUtils.isNull(req.getParameter("min")) ? serviceStarService.getCountAdvisor() : Integer.valueOf(req.getParameter("min")) + 3);
			param.put("openId", openId);
			param.put("input", input);
			map.put("resultList", serviceStarService.getAdvisor(param));
		} catch (Exception e) {
			logger.info("查询服务顾问信息失败！");
			logger.info(e.getMessage());
			map.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_ERROR);
			map.put(ResultMsgConstants.ERR_MSG, "查询服务顾问信息排序失败！");
			map.put(ResultMsgConstants.QUERY_DATA, "");
		}
		return map;
	}
	
	@RequestMapping(value = "/queryAdvisor", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public @ResponseBody Map<String, Object> queryAdvisor(HttpServletRequest req) { 
		Map<String, Object> map = new HashMap<>();
		try {
			String openId = req.getHeader(PointActivityConstant.HEADER_OPENID);
//			openId = null == openId ? "oXrcfuC1R4wosEPeJOHR7BPqaAOo" : openId;
			String input = "%"+req.getParameter("input").toString()+"%";
			String province = req.getParameter("province");
			Map<String, Object> param = new HashMap<>();
			param.put("openId", openId);
			param.put("input", input);
			param.put("province", province);
			map.put("resultList", serviceStarService.queryAdvisor(param));
			
		} catch (Exception e) {
			logger.info("查询服务顾问信息失败！");
			map.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_ERROR);
			map.put(ResultMsgConstants.ERR_MSG, "查询服务顾问信息失败！");
			map.put(ResultMsgConstants.QUERY_DATA, "");
		}
		
		return map;
	}
	
	@RequestMapping(value = "/getCountAdvisor", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public @ResponseBody Map<String, Object> getCountAdvisor(HttpServletRequest req) { 
		Map<String, Object> map = new HashMap<>();
		map.put("result", serviceStarService.getCountAdvisor());
		return map;
	}
	
	/**
	 * 每天重置投票次数与分享状态
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "/resetVoter", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	public @ResponseBody Map<String, Object> resetVoter(HttpServletRequest req) {
		Map<String, Object> map = new HashMap<>();
		try {
			map.put("updateBy", OPERATION_BY);
			map.put("updateDate", new Date());
			serviceStarService.resetVoter(map);
		} catch (Exception e) {
			logger.info("重置投票次数与分享状态失败！");
			map.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_ERROR);
			map.put(ResultMsgConstants.ERR_MSG, "重置投票次数与分享状态失败！");
			map.put(ResultMsgConstants.QUERY_DATA, "");
		}
		return map;
	}
	
	/**
	 * 分享可投票数加1
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "/doShare", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public @ResponseBody Map<String, Object> doShare(HttpServletRequest req) {
		Map<String, Object> map = new HashMap<>();
		try {
			String openId = req.getHeader(PointActivityConstant.HEADER_OPENID);
//			openId = null == openId ? "oXrcfuC1R4wosEPeJOHR7BPqaAOo" : openId;
			logger.info("服务之星活动获取到的openId为:=============" + openId);
			if(openId == null || "".equals(openId)){
				map.put("message", "1");
				return map;
				}
			boolean ifvote = serviceStarService.ifVoter(openId);
			if(!ifvote){
				map.put("message", "2");
				return map;
				}
			map.put("message", serviceStarService.doShare(openId));
		} catch (Exception e) {
			logger.info("分享后续操作失败！");
			map.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_ERROR);
			map.put(ResultMsgConstants.ERR_MSG, "分享后续操作失败！");
			map.put(ResultMsgConstants.QUERY_DATA, "");
			map.put("message", "0");
		}
		return map;
	}
	
	/**
	 * 投票
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "/doVote", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public @ResponseBody Map<String, Object> doVote(HttpServletRequest req) {
		Map<String, Object> map = new HashMap<>();
		Map<String, Object> param = new HashMap<>();
		try {
			String openId = req.getHeader(PointActivityConstant.HEADER_OPENID);
//			openId = null == openId ? "oXrcfuJyXpsFpkLDKQHh9QbQZb50" : openId;
			if(openId == null || "".equals(openId)){
				map.put("message", "0");
				return map;
			}
			boolean ifvote = serviceStarService.ifVoter(openId);
			if(!ifvote){
				map.put("message", "2");
				return map;
				}
			logger.info("服务之星活动获取到的openId为:=============" + openId);
			param.put("openId", openId);
			param.put("advisorId", req.getParameter("advisorId"));
			String message = serviceStarService.doVote(param);
			map.put("message", message);
		} catch (Exception e) {
			logger.info("投票操作失败！");
			map.put(ResultMsgConstants.RESULT_CODE, ResultMsgConstants.RESULT_CODE_ERROR);
			map.put(ResultMsgConstants.ERR_MSG, "投票操作失败！");
			map.put(ResultMsgConstants.QUERY_DATA, "");
		}
		return map;
	}
	
}
